package org.logtime.blog.controller.api;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.logtime.blog.annotation.Cache;
import org.logtime.blog.exception.ApiException;
import org.logtime.blog.interceptor.ApiInterceptor;
import org.logtime.blog.module.JsonModule;
import org.logtime.blog.module.PageModule;
import org.logtime.blog.pojo.Article;
import org.logtime.blog.pojo.User;
import org.logtime.blog.service.PersistService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @version: 1.0
 * @Author: itmins
 * @Email: itmins@gmail.com
 */
@Controller
@RequestMapping("/articles/")
public class ArticleController {

	@Cache(value = Cache.CACHE_CLEAR)
	@RequestMapping(value = "post.json")
	public JsonModule post(@ModelAttribute("article") Article article,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String accessIp = (String) request
				.getAttribute(ApiInterceptor.ACCESS_USER_IP_REQUEST_NAME);
		User accessUser = (User) request
				.getAttribute(ApiInterceptor.ACCESS_USER_REQUEST_NAME);
		persistService.save(this.factory(article, accessUser, accessIp));
		return JsonModule.getJson(article);
	}

	@RequestMapping(value = "get.json")
	public JsonModule get(@RequestParam("id") Long id,
			HttpServletRequest request, HttpServletResponse response) {
		return JsonModule.getJson(persistService.get(Article.class, id));
	}

	@Cache(value = Cache.CACHE_CLEAR)
	@RequestMapping(value = "delete.json")
	public JsonModule delete(@RequestParam("id") Long id,
			HttpServletRequest request, HttpServletResponse response) {
		persistService.delete(Article.class, id);
		return JsonModule.getJson(true);
	}

	@RequestMapping(value = "getAll.json")
	public JsonModule getAll() {
		PageModule<Article> result = persistService.get(Article.class);
		return JsonModule.getJson(result);
	}

	private Article factory(Article article, User accessUser, String accessIp) {
		if (null == article.getTitle() || "".equals(article.getTitle().trim())) {
			throw new ApiException(ApiException.DATA_VALIDATION_ERROR_CODE,
					"article title valdation false");
		}
		if (null == article.getContent()
				|| "".equals(article.getContent().trim())) {
			throw new ApiException(ApiException.DATA_VALIDATION_ERROR_CODE,
					"article content valdation false");
		}
		SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		article.setPosterId(accessUser.getId());
		article.setModifiedDate(simple.format(new Date()));
		if (null == article.getPostDate()) {
			article.setPostDate(article.getModifiedDate());
		}
		article.setPostFrom(accessIp);
		article.setStatus(Article.POSTER_ARTICLES_STATUS);
		return article;
	}

	@Resource
	private PersistService persistService;

	public PersistService getPersistService() {
		return persistService;
	}

	public void setPersistService(PersistService persistService) {
		this.persistService = persistService;
	}
}